﻿using UnityEngine;

public class TKChessBoardComponent : BaseComponent
{
    private GameObject chessCollection;
    private GameObject circleParent;
    private Transform smallCircleRT;
    private Transform largeCircleRT;
    public override void Binding()
    {
        chessCollection = transform.Find("ChessCollection").gameObject;
        circleParent = transform.Find("CircleParent").gameObject;
        smallCircleRT = transform.Find("CircleParent/SmallCircle").GetComponent<Transform>();
        largeCircleRT = transform.Find("CircleParent/LargeCircle").GetComponent<Transform>();
        TKChessBoardComponentModel cm = (TKChessBoardComponentModel)GetComponentModel();
        BindData(cm.posDM, (Vector3 value) =>
        {
            chessCollection.transform.localPosition = value;
        });
        BindData(cm.showCircle, (bool value) =>
        {
            circleParent.SetActive(value);
        });
        BindData(cm.smallCirclePos, (Vector3 value) =>
        {
            smallCircleRT.localPosition = value;
        });
        BindData(cm.largeCirclePos, (Vector3 value) =>
        {
            largeCircleRT.localPosition = value;
        });
        BindListComponent(cm.chessLCM, chessCollection, cm.chessLCM.ToArray, "TKChessPiece", typeof(TKChessComponent));
        PublicEventCenter.AddListener(UIManager.ClickSceneGameObjectEvent, ClickObject);
    }
    public override void UnBinding()
    {
        PublicEventCenter.RemoveListener(UIManager.ClickSceneGameObjectEvent, ClickObject);
        TKChessBoardComponentModel cm = (TKChessBoardComponentModel)GetComponentModel();
        cm.CancelComputerTryMove();
    }

    void Update()
    {
        TKChessBoardComponentModel cm = (TKChessBoardComponentModel)GetComponentModel();
        if (cm != null)
        {
            cm.ExecuteUpdate();

            if (Input.GetKeyDown(KeyCode.A))
            {
                cm.ComputerTryMove();
            }
        }
    }
    private void ClickObject(object[] objs)
    {
        if (objs[0] != null && objs[1] != null)
        {
            Debug.Log("" + objs[0] + "," + objs[1]);
            TKChessBoardComponentModel cm = (TKChessBoardComponentModel)GetComponentModel();
            if (!gameObject.Equals(null) && objs[0].Equals(gameObject))
            {
                cm.ClickOnBoard((Vector2)objs[1]);
            }
            else
            {
                if (!chessCollection.Equals(null))
                {
                    int count = chessCollection.transform.childCount;
                    for (int i = 0; i < count; i++)
                    {
                        GameObject child = chessCollection.transform.GetChild(i).gameObject;
                        if (!child.Equals(null) && objs[0].Equals(child))
                        {
                            TKChessComponent cc = child.GetComponent<TKChessComponent>();
                            TKChessComponentModel ccm;
                            if (cc != null && (ccm = (TKChessComponentModel)cc.GetComponentModel()) != null)
                            {
                                cm.ClickOnChess(ccm);
                            }
                            break;
                        }
                    }
                }
            }
        }
    }
}
